package com.zz.framework.core.base
import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.PersistableBundle
import androidx.activity.OnBackPressedCallback
import androidx.activity.enableEdgeToEdge
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.zz.framework.R
import com.zz.framework.core.ext.viewBinding
import com.zz.framework.core.toast.ToastUtils
import com.zz.framework.databinding.ActivityFragmentParentBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlin.properties.Delegates

/**
 * Copyright © 2020 zanezhao All rights reserved.
 * Created by zanezhao on 2020/3/1 10:08 PM.
 * @author: zanezhao 1820022519@qq.com
 * @version: V1.0
 */
@AndroidEntryPoint
class FragmentParentActivity : BaseActivity() {
    companion object {
        val KEY_FRAGMENT = "KEY_FRAGMENT"
    }
    var fragment: Fragment by Delegates.notNull()
    var baseFragment: BaseFragment by Delegates.notNull()

    private val binding: ActivityFragmentParentBinding by viewBinding()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        enableEdgeToEdge()
//        setOnApplyWindowInsetsPadding(binding.root)
        val clz = intent.getSerializableExtra(KEY_FRAGMENT) as Class<*>
        val cls = clz.name
        fragment = Fragment.instantiate(this, cls)
        if (fragment is BaseFragment) baseFragment = fragment as BaseFragment
        val ft: FragmentTransaction = supportFragmentManager.beginTransaction()
        ft.replace(binding.mContentLayout.id, fragment, cls)
        ft.commitAllowingStateLoss()
//        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
//            override fun handleOnBackPressed() {
//            }
//        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        baseFragment?.onActivityResult(requestCode, resultCode, data)
    }

    override fun onBackPressed() {
        if (baseFragment != null && baseFragment.onBackPressed()) {
            super.onBackPressed()
        }
    }
}